package com.tb.controller;


import com.github.pagehelper.PageInfo;
import com.tb.pojo.Student;
import com.tb.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stu")
public class StudentController {


    @Autowired
    private StudentService ss;

    /**
     * 学生注册
     * @param mav 返回的模型视图
     * @param student 学生信息
     * @return
     */
    @PostMapping("/register")
    public ModelAndView register(ModelAndView mav,
                                 Student student) {
//        System.out.println(student);
        ss.register(student);
        /*可以写一个注册成功的界面，我确实不想做了*/
        mav.setViewName("redirect:/stu/getList");
        return mav;
    }


    /**
     *
     * 学生注册页面的跳转
     * @param model 返回的数据
     * @return
     */
    @GetMapping("/toRegister")
    public String toRegister(Model model) {

        return "redirect:/info/getCollege";
    }


    @GetMapping("/getList")
    public ModelAndView getList(ModelAndView mav,
                                @RequestParam(value = "college", defaultValue = "0") int college,
                                @RequestParam(value = "profession", defaultValue = "0") int profession,
                                @RequestParam(value = "pg", defaultValue = "1") int currentPage) {
        PageInfo<Student> page;
        /*判断是否有college 和 profession 信息传入*/
        System.out.println(college);
        System.out.println(profession);
        if (profession == 0 && college == 0) {
            page = ss.findAll(currentPage);
        } else {
            page = ss.findAll(currentPage, college, profession);
        }

        mav.addObject("studentList", page.getList());
        mav.addObject("currentPage", page.getPageNum());
        mav.addObject("end", page.getEndRow());
        mav.addObject("pages", page.getPages());
        mav.setViewName("/student/information");
        return mav;
    }


    /**
     * 查看学生的详细信息
     * @param mav 返回的视图数据模型
     * @param sid 传入的学生的学号
     * @return
     */
    @GetMapping("/view")
    public ModelAndView view(ModelAndView mav,
                             @RequestParam(value = "sid") String sid) {
        Student stu = ss.findOne(sid);
        mav.addObject("student", stu);
        mav.setViewName("/student/view");
        return mav;
    }


    /**
     * 获取单体学生的信息 可以进行修改操作
     * @param mav
     * @param sid
     * @return
     */
    @GetMapping("/toModify")
    public ModelAndView toModify(ModelAndView mav,
                           @RequestParam(value = "sid") String sid) {
        Student stu = ss.findOne(sid);
        mav.addObject("student", stu);
        mav.setViewName("/student/modify");
        return mav;
    }


    /**
     * 学生信息修
     * @param mav 返回的视图数据
     * @param student 传入的需要修改的 student 数据
     * @return
     */
    @PostMapping("/modify")
    public ModelAndView modify(ModelAndView mav,
                               Student student) {
        /*此时拿到的student的profession数据为空 clazz数据包含了这个这个里面*/
        System.out.println(student);
        ss.modify(student);
        mav.setViewName("redirect:/stu/getList");
        return mav;
    }


}
